package com.dubito.dose.data.repository

import com.dubito.dose.data.MedicationDao
import com.dubito.dose.data.mapper.toMedication
import com.dubito.dose.data.mapper.toMedicationEntity
import com.dubito.dose.domain.model.Medication
import java.util.Date
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

/**
 * author: LiLiang
 * createTime: 2022/9/5-13:42
 * remark:
 **/
class MedicationRepositoryImpl(private val dao: MedicationDao) : MedicationRepository {

    override suspend fun insertMedication(medication: Medication) {
        dao.insertMedication(medication.toMedicationEntity())
    }

    override suspend fun deleteMedication(medication: Medication) {
        dao.deleteMedication(medication.toMedicationEntity())
    }

    override fun getMedicationForDate(date: Date): Flow<List<Medication>> {
        return dao.getMedicationsForDate(date = date)
            .map { entities -> entities.map { it.toMedication() } }
    }

    override fun getAllMedications(): Flow<List<Medication>> {
        return dao.getAllMedications()
            .map { entities -> entities.map { it.toMedication() } }
    }
}